//
//  TTPictureView.swift
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/25.
//  Copyright © 2016年 陶路路. All rights reserved.
//

import UIKit

class TTPictureView: UICollectionView {
    
    let pictureWidth:CGFloat = 80
    
    let margin:CGFloat = 5
    
    var tthomeModel:[TTPictureModel]?{
        
        didSet{
            
            let size = getSize()
            
            self.snp_updateConstraints{ (make) -> Void in
                
                make.size.equalTo(size);
            }
            
            reloadData()

        }
    }


    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        self.backgroundColor = UIColor.whiteColor()
        
        setUpUI()
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI(){
        
        dataSource = self
        let layout = self.collectionViewLayout as? UICollectionViewFlowLayout
        layout?.itemSize = CGSizeMake(pictureWidth, pictureWidth)
        layout?.minimumInteritemSpacing = margin
        layout?.minimumLineSpacing = margin
        registerClass(PictureCell.self, forCellWithReuseIdentifier: "cell")
        
    }
    
    func getSize()->CGSize{
        
        let num = tthomeModel?.count ?? 0
        // 行数
        let rol = num == 4 ? 2 : num >= 3 ? 3 : num
        // 列数
        let col = num == 4 ? 2 : ((num - 1) / 3) + 1
        
        let width = CGFloat(rol) * pictureWidth + CGFloat(rol - 1)*margin
        
        let hight = CGFloat(col) * pictureWidth + CGFloat(col - 1) * margin
        
        return CGSizeMake(width, hight)
    }
    
    
}


extension TTPictureView:UICollectionViewDataSource{
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return  tthomeModel?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as?PictureCell
        
//        cell!.backgroundColor = RandomColor()
        cell?.cellModel = tthomeModel?[indexPath.item]
        return cell!
    }
}


// 自定义cell

class PictureCell : UICollectionViewCell{
    
    var cellModel:TTPictureModel?{
        
        didSet{
            
            imageView.sd_setImageWithURL(NSURL(string: (cellModel?.thumbnail_pic)!))
            
            if let url:String = cellModel?.thumbnail_pic where url.hasSuffix(".gif"){
                
                gifImage.hidden = false
            }else{
                
                gifImage.hidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI(){
        
        contentView.addSubview(imageView)
        contentView.addSubview(gifImage)
        imageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImage.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(imageView)
            make.trailing.equalTo(imageView)
        }
    }
    
    lazy var imageView:UIImageView = {
       let image = UIImageView()
        image.image = UIImage(named: "weibo_placeholder")
        image.contentMode = .ScaleAspectFill
        image.layer.masksToBounds = true
        return image
    }()
    
    lazy var gifImage:UIImageView = {
       let gif = UIImageView()
        gif.image = UIImage(named: "timeline_image_gif")
        return gif
    }()
    
    
}